﻿using HelloWorld.Model;
using HelloWorld.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;


namespace HelloWorld.Controllers
{
    [Authorize]  // 添加Token检测校验
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HelloController : ControllerBase
    {

        private readonly ICalculateService _calculateService;

        // 注入日志
        private readonly ILogger<HelloController> _logger;

        public HelloController(ICalculateService calculateService, ILogger<HelloController> logger)
        {
            this._calculateService = calculateService;
            this._logger = logger;
        }

        [HttpGet]
        public async Task<List<HelloModel>> Get()
        {
            this._logger.LogWarning("Get Hello List");
            int result = await this._calculateService.Sum(1, 2);

            var list = new List<HelloModel>();
            list.Add(new HelloModel
            {
                Id = 1,
                Name = "bob",
            });

            return list;
        }

        [HttpPost]
        public IActionResult Post(TestDTO testdto)
        {
            // 校验参数
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            // 存储testdto
            this._logger.LogWarning($"提交参数: {0}", testdto.Name);
            return Ok();
        }
    }
}
